package kxfang.com.common.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import kxfang.com.base.livedatabus.LiveDatabus;
import kxfang.com.common.constant.Constant;
import kxfang.com.common.databus.EventCode;
import kxfang.com.common.databus.MastEvent;


/**
 * 微信支付回调
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.wx_pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.errCode == 0) {
				LiveDatabus.getInstance().with("支付回调！", MastEvent.class).setValue(new MastEvent<>(EventCode.PAY_WX_CODE, "支付成功"));
			} else if (resp.errCode == -2) {
				LiveDatabus.getInstance().with("支付回调！", MastEvent.class).setValue(new MastEvent<>(EventCode.PAY_WX_CODE1, "取消支付"));
			} else {
				LiveDatabus.getInstance().with("支付回调！", MastEvent.class).setValue(new MastEvent<>(EventCode.PAY_WX_CODE2, "支付失败"));
			}
		}
		finish();
	}
}